package com.oco.restcontroller.api.resttemplate.common;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 调用为服务工具类
 * @author zhangzhihong
 * @email it_javasun@yeah.net
 * @since 2016年8月10日上午9:55:29
 */
@Service
@Lazy(false)
// @ConditionalOnBean({RestTemplate.class})
public class RESTRibbon {

	@Resource
	private DiscoveryClient discoveryClient;
	
	@Resource
	@LoadBalanced //启用负载均衡策略
	private RestTemplate restTemplate;

	public RestTemplate restTemplate(){
		return restTemplate;
	}
	
	public String EXCHANGEURI(){
		List<ServiceInstance> sis = discoveryClient.getInstances(Constants.EXCHANGE_INSTANCE);
		if(CollectionUtils.isEmpty(sis)){
			return Constants.EXCHANGE_INSTANCE_URI;
		}
		return sis.get(0).isSecure() ? Constants.EXCHANGE_INSTANCE_SECURE_URI : Constants.EXCHANGE_INSTANCE_URI;
	}
}
